<?php
/**
 * wiki中间页
 * Class MiddleListAction
 */

use Biz\model\wiki\MGoodsAlbum;
use Biz\service\wiki\WikiMiddle;
use Doraemon\service\statSign\Basic as basicStatSign;
use Doraemon\tools\BlockType;

/**
 * 5.17.0
 * 6.2版本之前 清单专辑
 * @deprecated
 */
class AlbumMiddleListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    const IS_LOG_EXPOSE = 1;

    public function main()
    {
        $list = [];
        try {
            //每页多少数据
            $per_page = (int)$this->getRequest()->getPost('per_page', 6);
            //当前页
            $page = (int)$this->getRequest()->getPost('page', 1);
            $page = ($page > 1) ? $page : 1;
            //获取wiki_id集合
            $wiki_id = (int)$this->getRequest()->getPost('wiki_id', 0);
            $album_id = (int)$this->getRequest()->getPost('album_id', 0);


            $act_from = $this->getRequest()->getPost('act_from', '');//可以自定义。wiki_detail：wiki详情；goodtings_album:发现好物-好物清单；
            if ($wiki_id <= 0) {
                throw new \Exception('wiki参数错误');
            }
            $uid = $this->uid;
            $AlbumInfo = MGoodsAlbum::getInfo($album_id);
            if (empty($AlbumInfo)) {
                throw new \Exception('专辑不存在');
            }
            if (empty($AlbumInfo['wiki'])) {
                throw new \Exception('wiki缺失');
            }
            $wiki_ids = explode(',', $AlbumInfo['wiki']);
            //总条数
            $count = count($wiki_ids);
            $wiki_ids = array_diff($wiki_ids, [$wiki_id]);
            if ($wiki_ids && is_array($wiki_ids) && $page == 1) {
                array_unshift($wiki_ids, $wiki_id);
                //偏移量，当前页-1乘以每页显示条数
                $start = ($page - 1) * $per_page;
                //每页的wiki_id集合
                $wiki_ids_page = array_slice($wiki_ids, $start, $per_page);
            } else {
                //偏移量，当前页-1乘以每页显示条数
                $start = ($page - 1) * $per_page;
                //每页的wiki_id集合
                $wiki_ids_page = array_slice($wiki_ids, $start - 1, $per_page);
            }
            $list['total'] = $count ?? 0;
            $list['per_page'] = $per_page;
            $list['current_page'] = $page;

            $params = [
                'wiki_ids' => $wiki_ids_page,
                'uid' => $uid
            ];
            $data = WikiMiddle::getMiddleWikiList($params);
            //埋点
            $index = $start;
            foreach ($data as $key => $v) {
                $extend[basicStatSign::EXTEND_KEY_OBJ_ID] = $key;
                $extend[basicStatSign::EXTEND_KEY_OBJ_TYPE] = BlockType::TYPE_WIKI;
                $extend[basicStatSign::EXTEND_KEY_INDEX] = $index;
                $data[$key]['statSign'] = basicStatSign::genStatSign(basicStatSign::MODULE_WIKI_MIDDLE_CARD, $extend,
                    ['brand_id' => $v['brand_id'], 'album_id' => $album_id, 'act_from' => $act_from, 'wiki_id' => $wiki_id]);
                $index++;
            }

            $current = $page * $per_page;

            $list['is_over'] = ($count <= $current) ? 1 : 0;
            $list['list'] = array_values($data);

        } catch (\Exception $e) {
            return $this->setSuccess([], self::API_CODE_ERROR, $e->getMessage());
        }
        $this->setSuccess($list);
    }

}